package com.kris.dev3.ObserverModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  发布者 发布内容
 */
public class AliBook implements MySubject {


    private String bookName;
    private Date bookDate;

    List<Object> observers; // 观察者集合
    public AliBook() {
        observers = new ArrayList<Object>();
    }


    @Override
    public void addObserver(MyObserver observer) {
        observers.add(observer);// 新增观察者
    }

    @Override
    public void delObserver(MyObserver observer) {

        int len = observers.indexOf(observer);
        if (len>=0) {
            observers.remove(observer);// 移除观察者
        }
    }

    @Override
    public void notifyObserver(String bookName, Date bookDate) {
        if (observers.size()>0) {
            for (Object obj:observers) {
                MyObserver sb = (MyObserver) obj;
                sb.update(bookName,bookDate);// 通知更新
            }
        }
    }

    @Override
    public void setBookDetail(String bookName) {
        this.bookName = bookName;
        this.bookDate = new Date();// 当前时间
        this.notifyObserver(bookName,bookDate);// 提醒订阅者
    }
}
